package com.yhsimon.thread;


/**
 *
 * 多个线程操作同个对象
 * 发现问题：线程不安全，数据紊乱 （线程添加延时，效果更明显）
 */
public class ThreadTest3 implements Runnable{

    private int ticket=10;
    @Override
    public void run() {
        while(true){
            if(ticket<=0){
                break;
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"-->第"+(ticket--)+"票");
        }
    }

    public static void main(String[] args) {
        ThreadTest3 tickets = new ThreadTest3();

        new Thread(tickets,"张三").start();
        new Thread(tickets,"李四").start();
        new Thread(tickets,"王五").start();
    }
}
